const sharp = require('sharp');
const fs = require('fs');
const path = require('path');

const inputImagePath = './original.png'; // Path to your original image
const outputImagePath = './output.jpg'; // Path to save the processed image
const newText = 'Your New Text'; // The replacement text

// Dimensions and location for replacing the text
const textBox = {
    top: 50,        // Y-coordinate
    left: 50,       // X-coordinate
    width: 200,     // Width of the rectangle to cover original text
    height: 50      // Height of the rectangle to cover original text
};

// Create the SVG text overlay
const svgText = `
<svg width="${textBox.width}" height="${textBox.height}">
  <rect width="${textBox.width}" height="${textBox.height}" fill="white"/> <!-- Cover original text -->
  <text x="10" y="30" font-size="20" fill="black">${newText}</text> <!-- Add new text -->
</svg>
`;

sharp(inputImagePath)
  .composite([
    {
      input: Buffer.from(svgText),
      top: textBox.top,
      left: textBox.left
    }
  ])
  .toFile(outputImagePath)
  .then(() => {
    console.log('Image processed and saved at:', outputImagePath);
  })
  .catch(err => {
    console.error('Error processing image:', err);
  });
